<template>
  <view class="order-all-container">
    <view v-if="list.length==0" class="kongImage">
      <image :src="kongBg" mode="widthFix"></image>
      <text class="text-gray">没有更多订单拉！</text>
    </view>
    <view v-else>
      <u-list
          @scrolltolower="scrolltolower"
      >
        <u-list-item v-for="(item,index) in list" :key="index">
          <law-order-item :item="item" @confirmOrderCompleted="getList"></law-order-item>
        </u-list-item>
      </u-list>
    </view>

  </view>
</template>

<script>
import {getDay, isEmptyArray, formatPrice, isEmptyString, toast} from '@/utils/common'
import dict from "@/static/const/dict";
import bgBase64 from "@/static/const/bgBase64.js";
import consultAndServiceConst from "@/static/const/consultAndServiceConst";
import orderManagement from "@/static/const/orderManagement";
import lawOrderItem from "@/components/lawOrderItem";
import config from "@/config.js";

export default {
  name: "orderAll",
  components: {
    lawOrderItem
  },
  data() {
    return {
      list: [],
      queryParams: {
        current: 1,
        size: 10,
        orderState: '1'
      },
      //支付状态
      payStatus: dict.orderPayStatus,
      //订单类型
      orderType: dict.orderType,
      tips: '',//流程提示
      kongBg: bgBase64.noMoreDataBg,
    }
  },
  onLoad() {
  },
  onShow() {
    this.getList();
  },
  methods: {
    getList() {
      this.queryParams.current = 1;
      this.$store.dispatch("LawyerOrder", this.queryParams).then((res) => {
        this.list = res.data.records;
        this.list = this.formatList(this.list);
      });
    },
    formatList(list) {
      if (!isEmptyArray(list)) {
        list.forEach(item => {
          item.avatar=config.baseUrl+item.avatar;
          item.createTime = getDay(item.createTime);
          item.priceShow = formatPrice(item.price);
          item.orderType = this.orderType.find(item1 => item1.value == item.orderType).label;
          item.orderTime=item.orderTime+"分钟";
          item.consultingName = item.orderType + "-" + item.consultingName;
          if (isEmptyString(item.lawyerName)) {
            item.tips = "待分配承办人";
          } else {
            if (isEmptyString(item.lawyerPost)) {
              item.tips = "由" + item.lawyerName + "律师承办";
            } else {
              item.tips = "由" + item.lawyerName + "律师" + item.lawyerPost + "承办";
            }
          }
          if(isEmptyString(item.file)){
            item.imgList=[];
          }else{
            item.imgList=item.file.split(",");
          }
        })
      }
      return list;
    },
    scrolltolower() {
      this.loadMore();
    },
    //加载更多
    loadMore() {
      this.queryParams.current++;
      this.$store.dispatch("LawyerOrder", this.queryParams).then((res) => {
        let list = res.data.records;
        list = this.formatList(list);
        this.list = this.list.concat(list);
      });
    },
    //联系律师
    contactLawyer(item) {
      if (isEmptyString(item.lawyerPhone)) {
        this.$u.toast("律师暂未设置手机号");
        return;
      }
      // #ifdef MP-WEIXIN
      uni.makePhoneCall({
        phoneNumber: item.lawyerPhone
      });
      // #endif
    },
    //复制订单号
    paste(num) {
      // #ifndef H5
      uni.setClipboardData({
        data: num,
        success: function () {
          uni.showToast({
            title: '订单号复制成功'
          });
        }
      });
      // #endif
    },
    //付款
    pay(item) {
      if (item.way == '1') {
        this.$u.toast("请等待审核");
        return;
      }
      const that = this;
      this.$store
          .dispatch("GenerateOrder", {
            price: item.price,
            orderType: consultAndServiceConst.consultOrder,
            consultingId: this.id,
            orderTime: "15",
            payType: orderManagement.payType.weChatPayment,
          })
          .then((res) => {
            uni.requestPayment({
              provider: "wxpay",
              timeStamp: res.data.timeStamp,
              nonceStr: res.data.nonceStr,
              package: res.data.package,
              signType: res.data.signType,
              paySign: res.data.paySign,
              success: function (res) {
                console.log("success:" + JSON.stringify(res));
                toast("支付成功");
                that.getList();
              },
              fail: function (err) {
                toast("支付失败");
                that.getList();
              },
            });
          });
    },
    //去补充资料
    toAddInformation(item) {
      uni.navigateTo({
        url: '/pages/order/addInformation?id=' + item.id
      });
    }
  },
}
</script>

<style scoped lang="scss">
.order-all-container {
  width: 100%;
  height: 100%;
  .kongImage{
    width: 100%;
    height: 100vh;
    background-color: #FFFFFF;
    display: flex;
    flex-direction: column;
    align-items: center;
    padding-top: 100rpx;
  }
  .list {
    padding: 1rem;
    background-color: #FFFFFF;
    margin-top: 0.1rem;
    margin-bottom: 1rem;
    box-sizing: border-box;

    .top {
      .right {
        width: 100%;
        padding-left: 0.8rem;
        min-height: 4rem;
        display: flex;
        flex-direction: column;
        justify-content: space-around;

        .title-container {

          justify-content: space-between;

          .title {
            font-size: 0.8rem;
            color: black;
          }

          .status {
            font-size: 0.8rem;
            color: gray;
            margin-left: 0.5rem;
          }
        }

        .time {
          font-size: 0.6rem;
          color: #999999;
          padding-top: 0.5rem;
          padding-bottom: 0.5rem;
        }

        .flex-row-end {
          justify-content: flex-end;
          align-items: center;

          text {
            font-size: 0.6rem;
            color: gray;
          }

          .price {
            font-size: 1rem;
            color: #e43d00;
            margin-left: 0.5rem;
          }
        }

      }
    }

    .middle {
      width: fit-content;
      padding-bottom: 0.5rem;
    }

    .bottom {
      justify-content: space-between;
      align-items: center;
      padding-top: 0.5rem;

      .orderId {
        font-size: 0.7rem;
        color: #999999;
      }

      .btn-container {
        .btn {
          background-color: rgba(0, 0, 0, 0);
          width: 4rem;
          line-height: normal;
          padding: 0px;
          padding: 5px 0;
          font-size: 0.5rem;
        }

        .btn-left {
          box-sizing: border-box;
          border: 1px solid #e43d00;
          color: #e43d00;
        }

        .btn-right {
          box-sizing: border-box;
          border: 1px solid #999999;
          color: #999999;
          margin-left: 0.5rem;
        }
      }
    }
  }
}
</style>